package com.nchu.research.research.initializer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import javax.sql.DataSource;

/**
 * @author fujianjian
 * @since 2020/12/9 14:40
 */
@Configuration
public class IHRDataSourceInitializer {

    @Value("classpath:/init/sql/table_def_init.sql")
    private Resource functionScript;

    @Bean
    public DataSourceInitializer dataSourceInitializer(DataSource ihrDataSource) {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        // 设置数据源
        initializer.setDataSource(ihrDataSource);
        initializer.setDatabasePopulator(databasePopulator());
        return initializer;
    }

    private DatabasePopulator databasePopulator() {
        final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScripts(functionScript);
        //populator.setSeparator(";");
        return populator;
    }
}
